package org.jeecg.zhensi.customer.service.impl;

import org.jeecg.zhensi.customer.entity.ConsumablePurchase;
import org.jeecg.zhensi.customer.entity.ConsumablePurchaseZi;
import org.jeecg.zhensi.customer.mapper.ConsumablePurchaseZiMapper;
import org.jeecg.zhensi.customer.mapper.ConsumablePurchaseMapper;
import org.jeecg.zhensi.customer.service.IConsumablePurchaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 耗材采购订单
 * @Author: jeecg-boot
 * @Date:   2023-01-12
 * @Version: V1.0
 */
@Service
public class ConsumablePurchaseServiceImpl extends ServiceImpl<ConsumablePurchaseMapper, ConsumablePurchase> implements IConsumablePurchaseService {

	@Autowired
	private ConsumablePurchaseMapper consumablePurchaseMapper;
	@Autowired
	private ConsumablePurchaseZiMapper consumablePurchaseZiMapper;
	
	@Override
	@Transactional
	public void saveMain(ConsumablePurchase consumablePurchase, List<ConsumablePurchaseZi> consumablePurchaseZiList) {
		consumablePurchaseMapper.insert(consumablePurchase);
		if(consumablePurchaseZiList!=null && consumablePurchaseZiList.size()>0) {
			for(ConsumablePurchaseZi entity:consumablePurchaseZiList) {
				//外键设置
				entity.setConsumablePurchaseH(consumablePurchase.getId());
				consumablePurchaseZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ConsumablePurchase consumablePurchase,List<ConsumablePurchaseZi> consumablePurchaseZiList) {
		consumablePurchaseMapper.updateById(consumablePurchase);
		
		//1.先删除子表数据
		consumablePurchaseZiMapper.deleteByMainId(consumablePurchase.getId());
		
		//2.子表数据重新插入
		if(consumablePurchaseZiList!=null && consumablePurchaseZiList.size()>0) {
			for(ConsumablePurchaseZi entity:consumablePurchaseZiList) {
				//外键设置
				entity.setConsumablePurchaseH(consumablePurchase.getId());
				consumablePurchaseZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		consumablePurchaseZiMapper.deleteByMainId(id);
		consumablePurchaseMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			consumablePurchaseZiMapper.deleteByMainId(id.toString());
			consumablePurchaseMapper.deleteById(id);
		}
	}
	
}
